<template>
  <div class="white-film-stretching">
    <div class="switch-box">
      <el-switch v-model="whiteFilmStretching.open" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
      <span>白模拉伸</span>
    </div>

    <el-form ref="stretchingForm" :model="whiteFilmStretching" :rules="stretchingRules" label-width="80px"
      v-if="heightFieldList.length">
      <el-form-item label="选择图层">
        <el-select v-model="whiteFilmStretching.layerData" placeholder="选择图层" popper-class="query-field">
          <el-option v-for="(item, index) in layerDataList" :key="index" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="高度字段" prop="heightField">
        <el-select v-model="whiteFilmStretching.heightField" placeholder="选择字段" popper-class="query-field">
          <el-option v-for="item in heightFieldList.find(e => e.name == whiteFilmStretching.layerData).list"
            :key="item.name" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="放大倍数">
        <el-slider v-model="whiteFilmStretching.magnification" :min="1" :max="10" :step="0.1"></el-slider>
      </el-form-item>

      <el-form-item label="颜色">
        <el-color-picker v-model="whiteFilmStretching.color" popper-class="color-popper"></el-color-picker>
        <span class="color-value" v-text="whiteFilmStretching.color"></span>
      </el-form-item>

      <el-form-item label="透明度" class="opacity-label">
        <el-slider v-model="whiteFilmStretching.opacity" :min="0.1" :max="1" :step="0.1"></el-slider>
        <el-input-number v-model="whiteFilmStretching.opacity" controls-position="right" :min="0" :max="1"
          :step="0.1"></el-input-number>
      </el-form-item>

      <el-form-item>
        <el-button plain @click="close">取消</el-button>
        <el-button type="primary" @click="addWhiteFilm">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validateHeightField } from '@/assets/js/rules.js';
export default {
  props: ['selectLayerNode'],

  data () {
    return {
      selectNode: null,
      nodeAttribute: null,
      serverInfo: {},
      whiteFilmStretching: {
        layerData: '',
        open: true,
        heightField: '',
        magnification: 1,
        color: '#247de0',
        opacity: 1
      },
      stretchingRules: {
        heightField: { validator: validateHeightField, trigger: 'blur' }
      },
      layerDataList: [],
      heightFieldList: [],
    };
  },

  methods: {
    async createdData () {
      await this.getServerInfo();
      await this.getLengend();
      console.log(this.layerDataList);
    },

    getServerInfo () {
      const { serverType, serverUrl } = this.nodeAttribute;
      let serverName = '';
      let styleName = '';
      switch (serverType) {
        case 'WMTS':
          const wmtsServerNameReg = new RegExp("(?<=/1.0/).*?(?=/)");
          serverName = serverUrl.match(wmtsServerNameReg) != null ? serverUrl.match(wmtsServerNameReg)[0] : '';
          const wmtsStyleNameReg = new RegExp(`(?<=/${serverName}/).*?(?=/)`);
          styleName = serverUrl.match(wmtsStyleNameReg) != null ? serverUrl.match(wmtsStyleNameReg)[0] : '';
          break;

        case 'WMS':
          const wmsServerNameReg = new RegExp("(?<=/1.1.1/).*?(?=/)");
          serverName = serverUrl.match(wmsServerNameReg) != null ? serverUrl.match(wmsserverNameReg)[0] : '';
          const wmsStyleNameReg = new RegExp(`(?<=/${serverName}/).*?(?=/)`);
          styleName = serverUrl.match(wmsStyleNameReg) != null ? serverUrl.match(wmsStyleNameReg)[0] : '';
          break;

        case 'XYZ':
          const typeReg = new RegExp("(?<=/mapserver/).*?(?=/)");
          const type = serverUrl.match(typeReg) != null ? serverUrl.match(typeReg)[0] : '';
          const mapServerNameReg = new RegExp(`(?<=/${type}/).*(?=/)`);
          const mapStyleNameReg = new RegExp("(?<=styleId=)[^&]*");
          serverName = serverUrl.match(mapServerNameReg) != null ? serverUrl.match(mapServerNameReg)[0] : '';
          styleName = serverUrl.match(mapStyleNameReg) != null ? serverUrl.match(mapStyleNameReg)[0] : '';
          break;

        default:
          break;
      }
      this.serverInfo = { serverName, styleName };
      this.getLengend();
    },

    // 获取图层数据列表
    getLengend () {
      const { serverIpPort } = this.nodeAttribute;
      const { serverName, styleName } = this.serverInfo;
      if (!serverName || !styleName) return;
      this.$get(`${serverIpPort}/mapserver/styleInfo/${serverName}/${styleName}/layer.json`).then(res => {
        if (res && res.length) {
          this.layerDataList = res.map(e => { return e.name; });
          this.whiteFilmStretching.layerData = this.layerDataList[0];
          this.layerDataList.forEach(item => {
            this.getfieldList(item);
          });
        }
      }).catch(err => {
        console.log('获取字段列表报错：', err);
      });
    },

    // 获取图层字段列表
    getfieldList (layerData) {
      const { serverIpPort } = this.nodeAttribute;
      const { serverName } = this.serverInfo;
      if (!serverName) return;
      this.$get(`${serverIpPort}/mapserver/${serverName}/${layerData}/getinfo.json`).then(res => {
        if (res && res.fields && res.fields.length) {
          this.heightFieldList.push({
            name: layerData,
            list: res.fields.filter(e => e.name != 'geom')
          });

        }
      }).catch(err => {
        console.log('获取字段列表报错：', err);
      });
    },

    addWhiteFilm () {
      this.$refs.stretchingForm.validate((valid) => {
        if (!valid) return false;
        this.$emit('addWhiteFilm', { serverInfo: this.serverInfo, whiteFilmStretching: this.whiteFilmStretching });
      });
    },

    close () {
      this.$emit('close');
    }
  },

  created () {
    this.selectNode = this.selectLayerNode;
    this.nodeAttribute = this.selectLayerNode.attribute;
    const { whiteFilmStretching } = this.nodeAttribute;
    if (whiteFilmStretching) Object.assign(this.whiteFilmStretching, whiteFilmStretching);
    this.getServerInfo();
  }
}
</script>

<style lang="less" scoped>
.white-film-stretching {
  position: fixed;
  left: 306px;
  top: 94px;
  width: 350px;
  background-color: #333333;
  padding: 10px;
  box-sizing: border-box;
  z-index: 999;

  .switch-box {
    position: relative;
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #fff;

    span {
      margin-left: 5px;
    }

    .close-btn {
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      cursor: pointer;
    }
  }

  /deep/.el-form {
    margin-top: 10px;

    .el-form-item {
      display: flex;
      justify-content: space-between;

      &::before,
      &::after {
        display: none;
      }

      &:last-child {
        justify-content: flex-end;

        .el-form-item__content {
          justify-content: flex-end;
        }
      }

      .el-form-item__label {
        font-size: 14px;
        color: #fff;
      }

      .el-form-item__content {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: calc(100% - 90px);
        margin-left: 0px !important;

        &::before,
        &::after {
          display: none;
        }
      }

      .el-select {
        width: 100%;

        .el-input__inner {
          width: 100%;
          background: rgba(0, 0, 0, 0);
          font-size: 14px;
          color: #fff;
        }
      }

      .el-slider {
        width: 100%;
      }

      .el-color-picker {
        width: 100px;
        height: 32px;

        .el-color-picker__trigger {
          width: 100%;
          height: 100%;
        }
      }

      .color-value {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100px;
        height: 32px;
        border: 1px solid #e6e6e6;
        box-sizing: border-box;
        border-radius: 4px;
        font-size: 14px;
        color: #fff;
      }

      .el-input-number {
        width: 100px;
        height: 32px;

        .el-input-number__decrease,
        .el-input-number__increase,
        .el-input__inner {
          background-color: rgba(0, 0, 0, 0);
        }

        .el-input-number__decrease,
        .el-input-number__increase {
          &:hover {
            color: #6fa7e8;
          }
        }

        .el-input__inner {
          border: 1px solid #e6e6e6;
          font-size: 14px;
          color: #fff;

          &:hover {
            border: 1px solid #e6e6e6;
          }
        }
      }
    }

    .opacity-label {
      .el-form-item__content {
        .el-slider {
          width: 100px;
        }
      }
    }
  }
}
</style>